package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.OSinfo;
import com.ruoyi.pill.domain.SystemMysqlBackups;
import com.ruoyi.pill.service.SystemMysqlBackupsService;
import com.ruoyi.quartz.service.impl.SysJobServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component("storeDataTask")
public class StoreDataTask {

    @Value("${spring.datasource.druid.master.username}")
    String userName;
    @Value("${spring.datasource.druid.master.password}")
    String password;
    String database_name = "db_pss";

    String filePath;

    @Value("${spring.datasource.win-path}")
    String windowsPath;

    @Value("${spring.datasource.linux-path}")
    String linuxPath;

    @Value("${spring.datasource.mac-path}")
    String macPath;


    @Autowired
    private SystemMysqlBackupsService systemMysqlBackupsService;

    public void storeData(){

        if (OSinfo.isWindows()){
            filePath = windowsPath;
        }else if (OSinfo.isLinux()){
            filePath = linuxPath;
        }else {
            filePath = macPath;
        }
        // 数据库文件名称
        StringBuilder mysqlFileName = new StringBuilder()
                .append("pill-factory")
                .append("_")
                .append(DateUtil.format(new Date(), "yyyy-MM-dd-HH-mm-ss"))
                .append(".sql");

        // 备份命令
        StringBuilder cmd = new StringBuilder()
                .append("mysqldump ")
                .append("-h")
                .append("127.0.0.1")
                .append(" -u")
                .append(userName)
                .append(" -p")
                .append(password)
                .append(" ")
                // 排除MySQL备份表
                .append(database_name)
                .append(" pill_factory")
                .append(" > ")
                .append(filePath)
                .append(mysqlFileName);
        // 判断文件是否保存成功
        if (!FileUtil.exist(filePath)) {
            FileUtil.mkdir(filePath);
        }
        // 获取操作系统名称
//        String osName = System.getProperty("os.name").toLowerCase();
        String[] command = new String[0];
        if (OSinfo.isWindows()) {
            // Windows
            command = new String[]{"cmd", "/c", String.valueOf(cmd)};
        } else if (OSinfo.isLinux()){
            // Linux
            command = new String[]{"/bin/sh", "-c", String.valueOf(cmd)};
        }else if (OSinfo.isMacOSX() || OSinfo.isMacOS()){
            command = new String[]{"/bin/bash","-c",String.valueOf(cmd)};
        }
        SystemMysqlBackups backups = new SystemMysqlBackups();
        backups.setName(mysqlFileName.toString());
        backups.setCmd(command[0]+" "+command[1]+" "+command[2]);
        backups.setCreateTime(new Date());
        systemMysqlBackupsService.save(backups);
        // 获取Runtime实例
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            if (process.waitFor() == 0) {
                System.out.printf("Mysql 数据库备份成功,备份文件名：%s", mysqlFileName);
            } else {
               throw new RuntimeException("网络异常，数据库备份失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException( "网络异常，数据库备份失败");
        }
    }
}
